package arrAndstr;

/*
    移除元素
 */
public class T27 {

    public static void main(String[] args) {
        int[] arr = new int[]{0,1,2,2,3,0,4,2};

        System.out.println(removeElement(arr, 2));

        for (int i : arr) {
            System.out.print(i + " ");
        }
    }

    public static int removeElement(int[] nums, int val) {
        // 直接在原数组上覆盖
        int count = 0;
        int len = nums.length;
        int index = 0;
        for(int i=0; i<len; i++) {
            if(val == nums[i]) {
                count++;
            } else {
                nums[index++] = nums[i];
            }
        }

        return count;
    }



    public static void swap(int[] array, int index1, int index2) {
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    }
}
